1<?php

namespace app\admin\controller;

use think\Controller;

use think\Db;
use think\Request;

class Game extends Controller
{


    public function edit()
    {

      
        $id = input('id');
        $links = db('to_game')->find($id);
        $title = $links['title'];
        $url = $links['url'];
        $desc = $links['desc'];

        return $this->fetch('game/edit', [
            'id' => $id,
            'title' => $title,
            'url' => $url,
            'desc' => $desc

        ]);


    }

    public function edits(){
        $id = input('id');
        // dump($id);die;
        $links = db('to_game')->find($id);

        if (request()->isPost()) {
            $data = [
                'id' => $id,
                'title' => input('title'),
                'url' => input('url'),
                'desc' => input('desc')
            ];
    

            $validate = \think\Loader::validate('Links');
            if (!$validate->check($data)) {
                $this->error($validate->getError());
                die;
            }
            if (db('to_links')->update($data)) {
                $this->success('修改game成功',url('admin/game/lst'));

            } else {
                $this->error('修改game失败');
            }
            return;
        }
    }

    public function add()
    {
        return view('game/add');
    }

//    查询
    public function game_select()
    {
        $game = Db::name('to_game')->field(['id', 'title', 'url', 'desc'])->select();


        return $this->fetch('game/game', [
            'game' => $game,
        ]);
    }

    // 增加游戏
    public function addgame(Request $request)
    {

        $p = Request::instance()->post();
        $file = $request->file('url');
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        $html = $info->getSaveName();
        $html = str_replace('\\','/',$html);
        $data = [
            'title'=>$p['title'],
            'desc'=>$p['desc'],
            'url' => $html,
        ];

        $result = Db::table('to_game')->data($data)->insert();
        if($result > 0){
            return $this->success('添加成功',url('admin/game/game_select'));
        } else {
            return $this->error('添加失败');
        }


    }


    // 删除游戏链接
    public function del()
    {
        $id =input('id');


        if (db('to_game')->delete($id)) {

            $this->success('删除游戏成功', 'admin/game/game_select');
        } else {
            $this->error('删除游戏失败');
        }
        return;


    }


}
